package sample;

import javafx.scene.paint.Color;
import utils.screenCtrl.ScreenCtrlLinux;
import utils.screenCtrl.WindowStatus;
import utils.shortcutKey.ShortcutKey;
import utils.utilTime.UtilTime;
import utils.windowCatch.WindowCatch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class MouseListener extends Thread{
    public static double []frameRectangle = new double[4];
    public MouseListener(){
        this.start();
    }
    public void run(){
        super.run();
        String[] args = new String[]{"/bin/bash", "-c", "xev -root"};

        String output = "";
        String outputRecord;
        String []keyInfo;
        String needInfo;
        String distance;
        List<WindowStatus> allWindow;
        double windowX;
        double windowY;

        double windowWidth = 0;


        String drugWindow;


        String windowID = null;

        int calibration = 0;//用于校准窗口ID,有的时候焦点转换也会产生一两连续的个ConfigureNotify event导致窗口的错误判断，一般来说三个ConfigureNotify event说明用户正在拖动窗口

        try{
            Process process = Runtime.getRuntime().exec(args);
            BufferedReader infoReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while(true){
                drugWindow = CantSee.getChooseWindow();
                output = infoReader.readLine();
                if(output == null)
                    continue;
                keyInfo = output.split(", ");
                switch (keyInfo[0]){
                    case "ConfigureNotify event":
                        //读取窗口的坐标
                        output = infoReader.readLine();
                        if(drugWindow != null){
                            System.out.println("位置讀取：" + UtilTime.getUnixTimeSNow());
                            needInfo = output.split(", ")[2];
                            distance = needInfo.substring(needInfo.indexOf("(")+1,needInfo.indexOf(")"));
                            windowX = Double.valueOf(distance.split(",")[0]) + windowWidth/2;
                            windowY = Double.valueOf(distance.split(",")[1]) + 18;
                            System.out.println("顏色變換" + UtilTime.getUnixTimeSNow());
                            System.out.println(windowX + "-----" + windowY + "-----" + frameRectangle[0] + "-----" + frameRectangle[1] + "-----" + frameRectangle[2] + "-----" + frameRectangle[3]);
                            //判斷鼠標所在的分屏框是否發生改變，並修改分屏框顏色
                            if((windowX < frameRectangle[0] || windowY < frameRectangle[1] || windowX > frameRectangle[2] || windowY > frameRectangle[3] ) ||
                                    (frameRectangle[0] + frameRectangle[1] + frameRectangle[2] + frameRectangle[3]) == 0){
                                for(int i = 0;i < CantSee.getFrameListSize();i ++){
                                    frameRectangle = CantSee.getFrameRectangle(i);
                                    if(windowX > frameRectangle[0] && windowY > frameRectangle[1] && windowX < frameRectangle[2] && windowY < frameRectangle[3]){
                                        /*if (CantSee.getChooseFrame() != null){
                                            CantSee.getChooseFrame().setColor(Color.rgb(255 ,255 ,0));
                                        }*/
                                        CantSee.releaseAllFrame();
                                        CantSee.setChooseFrame(i);
                                        CantSee.getChooseFrame().setColor(Color.rgb(0 ,205 ,0));
                                        System.out.println("switch ------------------------------");
                                        break;
                                    }
                                    frameRectangle = new double[4];
                                }
                            }
                            System.out.println("結束" + UtilTime.getUnixTimeSNow());
                        }
                        outputRecord = output;
                        if(calibration < 3){
                            System.out.println("獲取窗口id" + UtilTime.getUnixTimeSNow());
                            output = infoReader.readLine();
                            System.out.println(output);
                            String windowID_save = output.split(", ")[1].split(" ")[1];
                            if(windowID == null){
                                windowID = windowID_save;
                                calibration++;
                            }else if(windowID.compareTo(windowID_save) != 0){
                                windowID = windowID_save;
                                calibration = 1;
                            }else{
                                calibration++;
                            }
                            if(calibration == 3){
                                String windowParent = output.split(", ")[1].split(" ")[1];
                                /*List<WindowStatus> windowStatusList = ScreenCtrlLinux.getAllWindowStatusStatic();
                                for(WindowStatus ws : windowStatusList){
                                    if(ws.getIdWithFrame().compareTo(windowParent) == 0){
                                        CantSee.setChooseWindow(ws.getId());
                                        windowWidth = Double.valueOf(ws.getWidth());

                                        break;
                                    }
                                }*/
                                System.out.println("獲取成功" + UtilTime.getUnixTimeSNow() + "窗口爲----------------->");
                                String catchWindow = WindowCatch.getSelectWindow();
                                if(catchWindow == null){
                                    calibration = 0;
                                    break;
                                }
                                CantSee.setChooseWindow(catchWindow);

                                windowWidth = Double.valueOf(outputRecord.split(", ")[3].split(" ")[1]);
//                                needInfo = outputRecord.split(", ")[2];
//                                distance = needInfo.substring(needInfo.indexOf("(")+1,needInfo.indexOf(")"));
//                                MouseLocation mouseLocationStatic = ScreenCtrlLinux.getMouseLocationStatic();
//                                correctionX = Double.valueOf(mouseLocationStatic.getX()) - Double.valueOf(distance.split(",")[0]) + 10;
//                                correctionY = Double.valueOf(mouseLocationStatic.getY()) - Double.valueOf(distance.split(",")[1]) + 10;
                            }

                        }
                        //读取被拖动的窗口ID
                        break;
/*                    case "UnmapNotify event":
                        if(calibration == 3){
                            MouseLocation mouseLocationStatic = ScreenCtrlLinux.getMouseLocationStatic();
                            CantSee.setChooseWindow(mouseLocationStatic.getId());
                            System.out.println(CantSee.getChooseWindow() + "<-------------------------------***");

                        }
                        break;*/
                    case "DestroyNotify event":
                        if(CantSee.getChooseFrame() == null)
                            break;
                        if(CantSee.getChooseWindow() == null)
                            break;
                        System.out.println("DestroyNotify event" + CantSee.getChooseWindow());
                        String chooseWindow = CantSee.getChooseWindow();
                        AppFrame chooseFrame = CantSee.getChooseFrame();
                        ScreenCtrlLinux.moveWindowToStatic(chooseWindow,(int)(chooseFrame.rectangle.getX() - CantSee.interval),(int)(chooseFrame.rectangle.getY() - CantSee.interval),(int)(chooseFrame.rectangle.getWidth() + 2*CantSee.interval),(int)(chooseFrame.rectangle.getHeight() + 2*CantSee.interval));
                        CantSee.setChooseWindow(null);
                        CantSee.setChooseFrame(null);
                        calibration = 0;
                        ShortcutKey.releaseShortoutKey();
                        frameRectangle = new double[4];
                        break;
                    case "EnterNotify event":
                    case "KeymapNotify event":
                    case "LeaveNotify event":
                    case "PropertyNotify event":
                        calibration = 0;
                        CantSee.setChooseWindow(null);
                        CantSee.setChooseFrame(null);
                        break;

                    default:
                        break;
                }
            }

        }catch (IOException e){
            e.printStackTrace();
        }

    }
}